/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.editor.forms;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.easyway.editor.commands.Command;
import org.easyway.system.StaticRef;

public class Navigator extends JPanel {

	private static final long serialVersionUID = 1L;

	private JButton reset = null;

	public float speed = 4f;

	/**
	 * This is the default constructor
	 */
	public Navigator() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(null);
		this.setSize(85, 85);
		this.setPreferredSize(new Dimension(85, 85));
		this.add(getReset(), null);
		this.add(getUp(), null);
		this.add(getLeft(), null);
		this.add(getDown(), null);
		this.add(getRight(), null);
		this.add(getZoomIn(), null);
		this.add(getZoomOut(), null);
		this.add(getDrawGrid(), null);
		this.add(getSwitchPanel(), null);
		this.add(getRight(), null);
	}

	/**
	 * This method initializes reset
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getReset() {
		if (reset == null) {
			reset = new JButton();
			reset.setPreferredSize(new Dimension(20, 20));
			reset.setLocation(new Point(3, 3));
			reset.setSize(new Dimension(24, 24));
			reset.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/origin.GIF")));
			reset.setText("");
			reset.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					StaticRef.getCamera().setx(0);
					StaticRef.getCamera().sety(0);
					new Command() {
						public void creation() {
							StaticRef.getCamera().zoom2D(1.0f);
							destroy();
						}

						public void loop() {
						}

						public void render() {
						}
					};
				}
			});
		}
		return reset;
	}

	Thread t;

	boolean can;

	private JToggleButton up = null;

	private JToggleButton down = null;

	private JToggleButton left = null;

	private JToggleButton right = null;

	private JButton zoomIn = null;

	private JButton zoomOut = null;

	private JToggleButton drawGrid = null;

	private JToggleButton switchPanel = null;

	private void moveCamera(final float speedx, final float speedy) {
		if (t == null) {
			can = true;
			(t = new Thread() {
				public void run() {
					while (can) {
						StaticRef.getCamera().move(speedx, speedy);
						try {
							Thread.sleep(10);
						} catch (Exception e) {
							e.printStackTrace();
						}
						Thread.yield();
					}
					t = null;
				}
			}).start();
		} else {
			can = false;
		}
	}

	/**
	 * This method initializes up
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getUp() {
		if (up == null) {
			up = new JToggleButton();
			up.setLocation(new Point(30, 3));
			up.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/arrowUp.GIF")));
			up.setSize(new Dimension(24, 24));
			up.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					moveCamera(0, -speed);
				}
			});
			up.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent e) {
					up.setSelected(true);
				}

				public void mouseReleased(java.awt.event.MouseEvent e) {
					up.setSelected(false);
				}
			});
		}
		return up;
	}

	/**
	 * This method initializes down
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getDown() {
		if (down == null) {
			down = new JToggleButton();
			down.setBounds(new Rectangle(30, 57, 24, 24));
			down.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/arrowDown.GIF")));
			down.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					moveCamera(0, speed);
				}
			});
			down.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent e) {
					down.setSelected(true);
				}

				public void mouseReleased(java.awt.event.MouseEvent e) {
					down.setSelected(false);
				}
			});
		}
		return down;
	}

	/**
	 * This method initializes left
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getLeft() {
		if (left == null) {
			left = new JToggleButton();
			left.setLocation(new Point(3, 30));
			left.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/arrowLeft.GIF")));
			left.setSize(new Dimension(24, 24));
			left.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					moveCamera(-speed, 0);
				}
			});
			left.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent e) {
					left.setSelected(true);
				}

				public void mouseReleased(java.awt.event.MouseEvent e) {
					left.setSelected(false);
				}
			});
		}
		return left;
	}

	/**
	 * This method initializes right
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getRight() {
		if (right == null) {
			right = new JToggleButton();
			right.setLocation(new Point(57, 30));
			right.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/arrowRight.GIF")));
			right.setSize(new Dimension(24, 24));
			right.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					moveCamera(speed, 0);
				}
			});
			right.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent e) {
					right.setSelected(true);
				}

				public void mouseReleased(java.awt.event.MouseEvent e) {
					right.setSelected(false);
				}
			});
		}
		return right;
	}

	/**
	 * This method initializes zoomIn
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getZoomIn() {
		if (zoomIn == null) {
			zoomIn = new JButton();
			zoomIn.setBounds(new Rectangle(57, 3, 24, 24));
			zoomIn.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/ZoomIn.GIF")));
			zoomIn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					zoom(-0.03f);
				}
			});
		}
		return zoomIn;
	}

	/**
	 * This method initializes zoomOut
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getZoomOut() {
		if (zoomOut == null) {
			zoomOut = new JButton();
			zoomOut.setBounds(new Rectangle(57, 57, 24, 24));
			zoomOut.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/ZoomOut.GIF")));
			zoomOut.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					zoom(+0.03f);
				}
			});
		}
		return zoomOut;
	}

	protected void zoom(final float factor) {

		new Command() {
			public void creation() {
				StaticRef.getCamera().zoom2D(
					StaticRef.getCamera().getZoom2D() + factor);
				destroy();
			}

			public void loop() {
			}

			public void render() {
			}
		};
	}

	/**
	 * This method initializes drawGrid
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getDrawGrid() {
		if (drawGrid == null) {
			drawGrid = new JToggleButton();
			drawGrid.setBounds(new Rectangle(3, 57, 24, 24));
			drawGrid.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/grid.GIF")));
			drawGrid.setSelected(true);
			drawGrid.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					EditorCore.drawGrid = drawGrid.isSelected();
				}
			});
		}
		return drawGrid;
	}
	
	public void setEnabled(boolean value) {
		reset.setEnabled(value);
		up.setEnabled(value);
		down.setEnabled(value);
		left.setEnabled(value);
		right.setEnabled(value);
		zoomIn.setEnabled(value);
		zoomOut.setEnabled(value);
		drawGrid.setEnabled(value);
	}

	/**
	 * This method initializes switchPanel	
	 * 	
	 * @return javax.swing.JToggleButton	
	 */
	private JToggleButton getSwitchPanel() {
		if (switchPanel == null) {
			switchPanel = new JToggleButton();
			switchPanel.setBounds(new Rectangle(30, 30, 24, 24));
			switchPanel.setIcon(new ImageIcon(getClass().getResource("/org/easyway/editor/forms/images/description.gif")));
			switchPanel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					UEditor.switchSouthComp();
				}
			});
		}
		return switchPanel;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
